using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Matrix.Configuration.DTOs;

namespace Matrix.Configuration.Contracts
{
    /// <summary>
    /// Matrix Configuration 统一配置管理应用服务接口
    /// 这是配置管理的核心接口，提供基础的配置管理功能
    /// </summary>
    public interface IConfigurationAppService : ICrudAppService<
        ConfigurationItemDto,
        Guid,
        GetConfigurationItemListInput,
        CreateConfigurationItemDto,
        UpdateConfigurationItemDto>
    {
        // === 基础配置操作 ===

        /// <summary>
        /// 通过键名获取配置值
        /// </summary>
        Task<ConfigurationItemDto> GetByKeyAsync(GetConfigurationItemInput input);

        /// <summary>
        /// 通过命名空间获取配置列表
        /// </summary>
        Task<ListResultDto<ConfigurationItemDto>> GetByNamespaceAsync(string @namespace);

        /// <summary>
        /// 检查配置是否存在
        /// </summary>
        Task<bool> ExistsAsync(GetConfigurationItemInput input);

        /// <summary>
        /// 通过键名删除配置
        /// </summary>
        Task DeleteByKeyAsync(GetConfigurationItemInput input);

        // === 类型化配置操作 ===

        /// <summary>
        /// 获取类型化配置值
        /// </summary>
        Task<T> GetTypedAsync<T>(GetConfigurationItemInput input);

        /// <summary>
        /// 设置类型化配置值
        /// </summary>
        Task SetTypedAsync<T>(GetConfigurationItemInput input, T value);

        // === 批量操作 ===

        /// <summary>
        /// 批量获取配置
        /// </summary>
        Task<Dictionary<string, ConfigurationItemDto>> GetBatchAsync(List<GetConfigurationItemInput> inputs);

        /// <summary>
        /// 批量设置配置
        /// </summary>
        Task<ListResultDto<ConfigurationItemDto>> SetBatchAsync(List<BatchConfigurationItemDto> configurations);
    }

    // === 扩展接口 ===

    /// <summary>
    /// 配置事件调试服务接口（独立接口，用于调试和监控）
    /// </summary>
    public interface IConfigurationEventDebugAppService : IApplicationService
    {
        Task<List<EventDebugDto>> GetRecentEventsAsync(int count = 50, string? eventType = null);
        Task<EventDebugDto> GetEventByIdAsync(Guid eventId);
        Task<EventReplayResultDto> ReplayEventAsync(Guid eventId);
        Task<EventStatisticsDto> GetEventStatisticsAsync();
        Task<List<EventDebugDto>> GetConfigurationHistoryAsync(string key, string @namespace = "", int maxCount = 50);
        Task<EventPerformanceStatsDto> GetPerformanceStatsAsync(string? eventType = null, int hours = 24);
    }

    /// <summary>
    /// 服务定义配置应用服务接口（独立接口，用于微服务配置管理）
    /// </summary>
    public interface IServiceDefinitionAppService : IApplicationService
    {
        Task DefineServiceAsync(DefineServiceInput input);
        Task<PagedResultDto<ServiceDefinitionDto>> GetServicesAsync(string filter = "", int maxResultCount = 10, int skipCount = 0);
        Task<ServiceConfigurationDefinitionDto> GetServiceConfigurationAsync(string serviceName);
        Task UpdateServiceAsync(string serviceName, UpdateServiceInput input);
        Task DeleteServiceAsync(string serviceName);
        Task<ServiceDependenciesDto> GetServiceDependenciesAsync(string serviceName);
    }

    // === 专门化服务接口 ===

    /// <summary>
    /// 配置查询服务接口
    /// 专门负责所有只读操作，提供高性能的查询能力
    /// </summary>
    public interface IConfigurationQueryService : IApplicationService
    {
        /// <summary>
        /// 通过键名获取配置值（带缓存）
        /// </summary>
        Task<ConfigurationItemDto> GetByKeyAsync(GetConfigurationItemInput input);

        /// <summary>
        /// 通过命名空间获取配置列表
        /// </summary>
        Task<ListResultDto<ConfigurationItemDto>> GetByNamespaceAsync(string @namespace);

        /// <summary>
        /// 检查配置是否存在
        /// </summary>
        Task<bool> ExistsAsync(GetConfigurationItemInput input);

        /// <summary>
        /// 获取类型化配置值
        /// </summary>
        Task<T> GetTypedAsync<T>(GetConfigurationItemInput input);

        /// <summary>
        /// 批量获取配置
        /// </summary>
        Task<Dictionary<string, ConfigurationItemDto>> GetBatchAsync(List<GetConfigurationItemInput> inputs);

        /// <summary>
        /// 分页获取配置列表
        /// </summary>
        Task<PagedResultDto<ConfigurationItemDto>> GetListAsync(GetConfigurationItemListInput input);

        /// <summary>
        /// 通过ID获取配置项
        /// </summary>
        Task<ConfigurationItemDto> GetAsync(Guid id);

        /// <summary>
        /// 清除指定配置的缓存
        /// </summary>
        Task ClearCacheAsync(string key, string @namespace = "");

        /// <summary>
        /// 清除命名空间的所有缓存
        /// </summary>
        Task ClearNamespaceCacheAsync(string @namespace);
    }

    /// <summary>
    /// 配置命令服务接口
    /// 专门负责所有写操作，包括创建、更新、删除和批量操作
    /// </summary>
    public interface IConfigurationCommandService : IApplicationService
    {
        /// <summary>
        /// 创建配置项
        /// </summary>
        Task<ConfigurationItemDto> CreateAsync(CreateConfigurationItemDto input);

        /// <summary>
        /// 更新配置项
        /// </summary>
        Task<ConfigurationItemDto> UpdateAsync(Guid id, UpdateConfigurationItemDto input);

        /// <summary>
        /// 删除配置项
        /// </summary>
        Task DeleteAsync(Guid id);

        /// <summary>
        /// 通过键名删除配置
        /// </summary>
        Task DeleteByKeyAsync(GetConfigurationItemInput input);

        /// <summary>
        /// 设置类型化配置值
        /// </summary>
        Task SetTypedAsync<T>(GetConfigurationItemInput input, T value);

        /// <summary>
        /// 批量设置配置
        /// </summary>
        Task<ListResultDto<ConfigurationItemDto>> SetBatchAsync(List<BatchConfigurationItemDto> configurations);
    }

    /// <summary>
    /// 配置缓存服务接口
    /// 专门负责缓存逻辑，提供高性能的缓存管理能力
    /// </summary>
    public interface IConfigurationCacheService : IApplicationService
    {
        /// <summary>
        /// 获取缓存项
        /// </summary>
        Task<ConfigurationItemDto?> GetAsync(string key, string @namespace = "");

        /// <summary>
        /// 设置缓存项
        /// </summary>
        Task SetAsync(string key, ConfigurationItemDto value, string @namespace = "", TimeSpan? expiration = null);

        /// <summary>
        /// 移除缓存项
        /// </summary>
        Task RemoveAsync(string key, string @namespace = "");

        /// <summary>
        /// 清除指定命名空间的所有缓存
        /// </summary>
        Task ClearNamespaceAsync(string @namespace);

        /// <summary>
        /// 清除所有配置缓存
        /// </summary>
        Task ClearAllAsync();

        /// <summary>
        /// 批量获取缓存项
        /// </summary>
        Task<Dictionary<string, ConfigurationItemDto>> GetBatchAsync(List<string> keys, string @namespace = "");

        /// <summary>
        /// 批量设置缓存项
        /// </summary>
        Task SetBatchAsync(Dictionary<string, ConfigurationItemDto> items, string @namespace = "", TimeSpan? expiration = null);

        /// <summary>
        /// 检查缓存项是否存在
        /// </summary>
        Task<bool> ExistsAsync(string key, string @namespace = "");

        /// <summary>
        /// 刷新缓存项（延长过期时间）
        /// </summary>
        Task RefreshAsync(string key, string @namespace = "", TimeSpan? expiration = null);

        /// <summary>
        /// 获取缓存统计信息
        /// </summary>
        Task<CacheStatisticsDto> GetStatisticsAsync();
    }

    // === 向后兼容接口（标记为废弃）===
    // 向后兼容接口已移至INamespaceConfigurationAppService.cs文件中
    // 这样可以避免重复定义和编译错误
}